<template>
  <view class="search-container">
    <view class="search-box">
      <uni-search-bar
        @input="input"
        radius="100"
        placeholder="请输入要搜索的内容"
        cancelButton="none"
        @clear="searchGoodsList = []"
      />
    </view>

    <!-- 搜索结果 -->
    <view v-if="searchGoodsList.length > 0" class="sugg-list">
      <view
        class="sugg-item"
        v-for="item in searchGoodsList"
        :key="item.goods_id"
        @click="goToDetail(item.goods_id)"
      >
        <text class="goods-name">{{ item.goods_name }}</text>
      </view>
    </view>
    <view v-else class="history-box">
      <view class="history-title">
        <text>搜索历史</text>
        <uni-icons type="trash" size="17" @click="clear"></uni-icons>
      </view>
      <view class="history-list">
        <uni-tag
          v-for="item in searchHistoryList"
          :key="item"
          :text="item"
          :inverted="true"
          @click="goToGoodsList(item)"
        />
      </view>
    </view>
  </view>
</template>

<script>
import { getSearchResultApi } from '@/api/index'
export default {
  data () {
    return {
      timer: null,
      searchGoodsList: [],
      searchHistoryList: uni.getStorageSync('history') || []
    };
  },

  methods: {
    goToGoodsList (query) {
      uni.navigateTo({ url: '/subpkg/goods-list/index?query=' + query })
    },

    clear () {
      this.searchHistoryList = []
      uni.setStorageSync('history', [])
    },

    goToDetail (id) {
      uni.navigateTo({ url: '/subpkg/goods-detail/index?goods_id=' + id })
    },

    input (e) {
      if (this.timer) {
        clearTimeout(this.timer)
      }
      this.timer = setTimeout(() => {
        this.getSearchResul(e)
      }, 500);
    },

    async getSearchResul (e) {
      if (e.trim().length === 0) return
      // 保存搜索历史
      this.saveHistory(e)
      const { meta: { status }, message } = await getSearchResultApi({ query: e })
      if (status === 200) {
        this.searchGoodsList = message
      }
    },

    saveHistory (e) {
      const findIndex = this.searchHistoryList.findIndex(item => item === e)
      if (findIndex > -1) {
        this.searchHistoryList.splice(findIndex, 1)
      }

      this.searchHistoryList.unshift(e)
      if (this.searchHistoryList.length > 10) {
        this.searchHistoryList.splice(10)
      }

      // 保存搜索历史
      uni.setStorageSync('history', this.searchHistoryList)

      // this.searchHistoryList.unshift(e)
      // this.searchHistoryList = [...new Set(this.searchHistoryList)];
      // if (this.searchHistoryList.length > 5) {
      //   this.searchHistoryList.splice(5)
      // }

    }

  }
}
</script>

<style lang="scss" scoped>
.search-container {
  .search-box {
    position: sticky;
    top: 0;
    z-index: 999;
    ::v-deep .uni-searchbar {
      /* #ifndef APP-NVUE */
      display: flex;
      /* #endif */
      flex-direction: row;
      position: relative;
      padding: 16rpx;
      /* 将默认的 #FFFFFF 改为 #C00000 */
      background-color: #c00000;
    }
  }

  .history-box {
    padding: 0 5px;

    .history-title {
      display: flex;
      justify-content: space-between;
      align-items: center;
      height: 40px;
      font-size: 13px;
      border-bottom: 1px solid #efefef;
    }

    .history-list {
      display: flex;
      flex-wrap: wrap;

      ::v-deep .uni-tag {
        margin-top: 5px;
        margin-right: 5px;
      }
    }
  }

  .sugg-list {
    padding: 0 5px;

    .sugg-item {
      font-size: 12px;
      padding: 13px 0;
      border-bottom: 1px solid #efefef;
      display: flex;
      align-items: center;
      justify-content: space-between;

      .goods-name {
        // 文字不允许换行（单行文本）
        white-space: nowrap;
        // 溢出部分隐藏
        overflow: hidden;
        // 文本溢出后，使用 ... 代替
        text-overflow: ellipsis;
        margin-right: 3px;
      }
    }
  }
}
</style>